package com.xiaoxiong.mami.service;

import com.xiaoxiong.mami.dao.daomain.UserToken;
import com.xiaoxiong.mami.dao.mapperImpl.UserTokenMapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by Administrator on 2017/8/8.
 */
@Service
public class TokenService implements UserTokenMapperImpl {

    @Autowired
    UserTokenMapperImpl tokenMapperImpl;

    @Override
    public UserToken queryTokenByToken(String token) {
        return tokenMapperImpl.queryTokenByToken(token);
    }

    @Override
    public UserToken queryTokenByRefreshToken(String refreshToken) {
        return tokenMapperImpl.queryTokenByToken(refreshToken);
    }

    @Override
    public UserToken queryTokenByUserId(String userId) {
        return tokenMapperImpl.queryTokenByUserId(userId);
    }

    @Override
    public int queryTokenCount(String userId) {
        return tokenMapperImpl.queryTokenCount(userId);
    }

    public int deleteToken(String userId) {
        return tokenMapperImpl.deleteToken(userId);
    }

    @Override
    public UserToken queryTokenByTokenAndRefreshToken(String token) {
        return tokenMapperImpl.queryTokenByTokenAndRefreshToken(token);
    }

    @Override
    public int insertUserToken(UserToken userToken) {
        return tokenMapperImpl.insertUserToken(userToken);
    }

    @Override
    public int updateUserToken(String userId, String token) {
        return tokenMapperImpl.updateUserToken(userId, token);
    }


    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public int insert(UserToken record) {
        return 0;
    }

    @Override
    public int insertSelective(UserToken record) {
        return 0;
    }

    @Override
    public UserToken selectByPrimaryKey(Long id) {
        return null;
    }


    @Override
    public int updateByPrimaryKeySelective(UserToken record) {
        return 0;
    }


    @Override
    public int updateByPrimaryKey(UserToken record) {
        return 0;
    }


    public boolean checkTokenIsVail(UserToken userToken) {
        long expireTime = userToken.getExpireTime();
        long tokenTime = userToken.getUpdateTime().getTime() + expireTime;
        long vailTime = tokenTime - System.currentTimeMillis();
        if (vailTime > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean checkTokenIsVailByTokenStr(String token) {
        UserToken userToken = tokenMapperImpl.queryTokenByToken(token);
        if (userToken == null) {
            return false;
        }
        long expireTime = userToken.getExpireTime();
        long tokenTime = userToken.getUpdateTime().getTime() + expireTime;
        long vailTime = tokenTime - System.currentTimeMillis();
        if (vailTime > 0) {
            return true;
        } else {
            return false;
        }
    }

}
